﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace FolderBrowsing
{
    public partial class Form1 : Form
    {
        private ToolStripLabel pathLabel;
        public Form1()
        {
            InitializeComponent();
            InitializeFileSystemTree();
            InitializeMenuStrip();
            InitializeListView(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)); // 传递用户个人文件夹路径
            InitializeToolStrip();

        }
        private void InitializeToolStrip()
        {
            toolStrip1 = new ToolStrip();
            Controls.Add(toolStrip1);
            ToolStripButton saveButton = new ToolStripButton();
            saveButton.Image = Image.FromFile("E:\\桌面\\picture\\save.png");  // 设置图标
            saveButton.Click += SaveButton_Click;  // 设置事件处理
            toolStrip1.Items.Add(saveButton);

            ToolStripDropDownButton settingsButton = new ToolStripDropDownButton();
            settingsButton.DropDownItems.Add("选项1", null, Option1_Click);
            settingsButton.DropDownItems.Add("选项2", null, Option2_Click);
            toolStrip1.Items.Add(settingsButton);

            ToolStripSeparator separator = new ToolStripSeparator();
            toolStrip1.Items.Add(separator);

            ToolStripLabel label = new ToolStripLabel("状态：就绪");
            toolStrip1.Items.Add(label);

            ToolStripProgressBar progressBar = new ToolStripProgressBar();
            progressBar.Value = 100;  
            toolStrip1.Items.Add(progressBar);
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            // 保存文件的代码
        }

        private void Option1_Click(object sender, EventArgs e)
        {
            // 选项1的处理代码
        }

        private void Option2_Click(object sender, EventArgs e)
        {
            // 选项2的处理代码
        }
        private void InitializeListView(string directoryPath)
        {
            listView1.View = View.Details;  // 设置视图为详细信息模式
            listView1.Columns.Clear();
            listView1.Columns.Add("名称", 150);  // 添加列
            listView1.Columns.Add("类型", 100);
            listView1.Columns.Add("最后修改时间", 150);
            listView1.ItemActivate += new EventHandler(this.listView_DoubleClick);

            // 使用传递进来的目录路径创建 DirectoryInfo 对象
            DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);

            foreach (DirectoryInfo dir in directoryInfo.GetDirectories())
            {
                ListViewItem item = new ListViewItem(dir.Name);
                item.SubItems.Add("文件夹");
                item.SubItems.Add(dir.LastWriteTime.ToString());
                listView1.Items.Add(item);
            }

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                ListViewItem item = new ListViewItem(file.Name);
                item.SubItems.Add("文件");
                item.SubItems.Add(file.LastWriteTime.ToString());
                listView1.Items.Add(item);
            }
        }


        private void InitializeMenuStrip()
        {
            // 创建菜单栏
            MenuStrip menuStrip = new MenuStrip();

            // 创建一个 ToolStripLabel 来显示路径
            pathLabel = new ToolStripLabel();
            pathLabel.Text = "当前路径：[未选择]";  // 初始文本

            // 将标签添加到 MenuStrip
            menuStrip.Items.Add(pathLabel);

            // 将 MenuStrip 添加到窗体的控件集
            this.MainMenuStrip = menuStrip;
            this.Controls.Add(menuStrip);
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        private void InitializeFileSystemTree()
        {
            TreeNode rootNode = new TreeNode(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
            rootNode.Tag = rootNode.Text;
            treeView1.Nodes.Add(rootNode);
            FillTreeNode(rootNode);
            treeView1.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(this.treeView_NodeMouseDoubleClick);
            treeView1.AfterSelect += treeView_AfterSelect;  
        }
        private void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (Directory.Exists(e.Node.Tag.ToString()))
            {
                e.Node.Toggle();
                if (e.Node.Nodes.Count == 0)
                {
                    FillTreeNode(e.Node); 
                    e.Node.Toggle();
                }
            }
        }
        private void FillTreeNode(TreeNode treeNode)
        {
            treeNode.Nodes.Clear(); // 清除旧的子节点（如果有）

            string path = treeNode.Tag.ToString(); // 获取父节点的路径
            DirectoryInfo directoryInfo = new DirectoryInfo(path);

            // 遍历子目录
            foreach (DirectoryInfo dir in directoryInfo.GetDirectories())
            {
                TreeNode dirNode = new TreeNode(dir.Name);
                dirNode.Tag = dir.FullName; // 使用完整路径作为Tag
                dirNode.Name = dir.FullName; // 这是查找节点时使用的键
                treeNode.Nodes.Add(dirNode); // 添加到树中
            }
        }
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                pathLabel.Text = "当前路径：" + e.Node.Tag.ToString();  // 更新标签显示当前选择的路径

                // 这里是其余已有的代码，用于更新 ListView 等
                listView1.Items.Clear();
                string selectedPath = e.Node.Tag.ToString();
                try
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(selectedPath);
                    foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
                    {
                        ListViewItem item = new ListViewItem(subdir.Name, 0);
                        item.Tag = subdir.FullName;
                        item.SubItems.Add("文件夹");
                        item.SubItems.Add(subdir.LastWriteTime.ToString());
                        listView1.Items.Add(item);
                    }
                    foreach (FileInfo file in directoryInfo.GetFiles())
                    {
                        ListViewItem item = new ListViewItem(file.Name); // 'file.Name'包含了扩展名
                        item.Tag = file.FullName; // 完整的文件路径用于打开文件
                        item.SubItems.Add("文件");
                        item.SubItems.Add(file.LastWriteTime.ToString());
                        listView1.Items.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("无法访问: " + ex.Message);
                }
            }
        }
        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                var selectedItem = listView1.SelectedItems[0];
                string selectedPath = selectedItem.Tag.ToString();

                if (Directory.Exists(selectedPath))
                {
                    // 如果是目录，则在TreeView中找到并展开该节点
                    TreeNode[] nodes = treeView1.Nodes.Find(selectedPath, true);
                    if (nodes.Length > 0)
                    {
                        treeView1.SelectedNode = nodes[0];
                        nodes[0].Expand();
                    }
                }
                else if (File.Exists(selectedPath) && Path.GetExtension(selectedPath).ToLower() == ".txt")
                {
                    // 如果是.txt文件，则打开它
                    System.Diagnostics.Process.Start("notepad.exe", selectedPath);
                }
            }
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}
